/*****************************************************************************
*
* Atmel Corporation
*
* File              : main.c
* Compiler          : IAR EWAAVR 2.28a/3.10c
* Revision          : $Revision: 2516 $
* Date              : $Date: 2007-09-27 10:41:15 +0200 (to, 27 sep 2007) $
* Updated by        : $Author: mlarsson $
*
* Support mail      : avr@atmel.com
*
* Supported devices : All devices with a TWI module can be used.
*                     The example is written for the ATmega16
*
* AppNote           : AVR311 - TWI Slave Implementation
*
* Description       : Example of how to use the driver for TWI slave 
*                     communication.
*
****************************************************************************/
/*! \page MISRA
 *
 * General disabling of MISRA rules:
 * * (MISRA C rule 1) compiler is configured to allow extensions
 * * (MISRA C rule 111) bit fields shall only be defined to be of type unsigned int or signed int
 * * (MISRA C rule 37) bitwise operations shall not be performed on signed integer types
 * As it does not work well with 8bit architecture and/or IAR

 * Other disabled MISRA rules
 * * (MISRA C rule 109) use of union - overlapping storage shall not be used
 * * (MISRA C rule 61) every non-empty case clause in a switch statement shall be terminated with a break statement
*/

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "TWI_Slave.h"



//**************************************************************************************************************************************************************

unsigned char messageBuf[MSG_BUFFER_SIZE];
unsigned char TWI_slaveAddress = 0xE0;

unsigned char TWI_Act_On_Failure_In_Last_Transmission ( unsigned char TWIerrorMsg );  // When there has been an error, this function is run and takes care of it

//**************************************************************************************************************************************************************

void main( void )
{
	

	
	TWI_Slave_Initialise( TWI_slaveAddress ); // Initialise TWI module for slave operation. Include address and/or enable General Call.

                       
    sei();

    TWI_Start_Transceiver();				   	          // Start the TWI transceiver to enable reception of the first command from the TWI Master.

  // This example is made to work together with the AVR315 TWI Master application note. In addition to connecting the TWI
  // pins, also connect PORTB to the LEDS. The code reads a message as a TWI slave and acts according to if it is a 
  // general call, or an address call. If it is an address call, then the first byte is considered a command byte and
  // it then responds differently according to the commands.



  while(1)											      // This loop runs forever. If the TWI is busy the execution will just continue doing other operations.
  {   
	  if ( ! TWI_Transceiver_Busy() )						  // Check if the TWI Transceiver has completed an operation.                    
	  {
		  if ( TWI_statusReg.lastTransOK )					  // Check if the last operation was successful
		  {
			  if ( TWI_statusReg.RxDataInBuf )				  // Check if the last operation was a reception
			  {
				  TWI_Get_Data_From_Transceiver(messageBuf, 2);         
			  }                
			  else											  // Ends up here if the last operation was a transmission  
			  {
			  }
             
			  if ( ! TWI_Transceiver_Busy() )				 	  // Check if the TWI Transceiver has already been started. If not then restart it to prepare it for new receptions. 
			  {
				  TWI_Start_Transceiver();
			  }
		 }
         else												  // Ends up here if the last operation completed unsuccessfully
         {
			 TWI_Act_On_Failure_In_Last_Transmission( TWI_Get_State_Info() );
		 }
      }
  }
}

//**************************************************************************************************************************************************************

unsigned char TWI_Act_On_Failure_In_Last_Transmission ( unsigned char TWIerrorMsg )
{
	/* A failure has occurred, use TWIerrorMsg to determine the nature of the failure and take appropriate actions.See header file for a list of possible 
	failures messages. This very simple example puts the error code on PORTB and restarts the transceiver with all the same data in the transmission buffers.*/
	PORTB = TWIerrorMsg;
    TWI_Start_Transceiver();
                    
    return TWIerrorMsg; 
}

//**************************************************************************************************************************************************************

/*  
  // A simplified example.
  // This will store data received on PORTB, and increment it before sending it back.

  TWI_Start_Transceiver();    
         
  while(1)
  {
	  unsigned char temp;
    if ( ! TWI_Transceiver_Busy() )                              
    {
      if ( TWI_statusReg.RxDataInBuf )
      {
        TWI_Get_Data_From_Transceiver(&temp, 1);  
        PORTB = temp;
      }
      temp = PORTB + 1;
      TWI_Start_Transceiver_With_Data(&temp, 1); 
    }
      // Do something else while waiting
  }
}
*/

